/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.toolbox_basics_project.spring.manager.impl;

import java.util.Locale;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.genia.toolbox.basics.exception.BundledException;
import com.genia.toolbox.persistence.bean.Persistable;
import com.genia.toolbox.persistence.dao.AbstractDAO;
import com.genia.toolbox.persistence.exception.PersistenceException;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.I18nDynamicString;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.I18nDynamicText;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.I18nDynamicValue;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.I18nString;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.I18nText;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.I18nValue;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.Language;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.impl.I18nStringImpl;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.impl.I18nTextImpl;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.I18nDynamicEntityManager;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.LanguageManager;

/**
 * an implementation of {@link I18nDynamicEntityManager}.
 */
public class I18nDynamicEntityManagerImpl
    extends AbstractDAO
    implements I18nDynamicEntityManager
{

  /**
   * the {@link LanguageManager} to use.
   */
  private LanguageManager languageManager;



  /**
   * returns the translation of this {@link I18nDynamicValue}.
   * 
   * @param i18nDynamicValue
   *          the {@link I18nDynamicValue}.
   * @return the translation of this {@link I18nDynamicValue}.
   * @throws PersistenceException
   *           when an error occured
   */
  public String getTranslation(I18nDynamicValue i18nDynamicValue)
      throws PersistenceException
  {
    if (i18nDynamicValue == null) {
      return null;
    }
    if (i18nDynamicValue.getI18nValues().isEmpty()) {
      return null;
    }
    Language currentLanguage = getLanguageManager().getCurrentLanguage();
    if (currentLanguage == null) {
      return null;
    }
    I18nValue i18nValue = i18nDynamicValue.getI18nValues().get(currentLanguage);
    if (i18nValue != null && StringUtils.hasText(i18nValue.getValue())) {
      return i18nValue.getValue();
    }
    Language defaultLanguage = getLanguageManager().getDefaultLanguage();
    if (defaultLanguage == currentLanguage) {
      return "";
    }
    i18nValue = i18nDynamicValue.getI18nValues().get(defaultLanguage);
    if (i18nValue != null && StringUtils.hasText(i18nValue.getValue())) {
      return i18nValue.getValue();
    }
    return "";
  }



  /**
   * getter for the languageDAO property.
   * 
   * @return the languageDAO
   */
  public LanguageManager getLanguageManager()
  {
    return languageManager;
  }



  /**
   * setter for the languageDAO property.
   * 
   * @param languageDAO
   *          the languageDAO to set
   */
  public void setLanguageManager(LanguageManager languageDAO)
  {
    this.languageManager = languageDAO;
  }



  /**
   * return the {@link I18nValue} associated to the given {@link Language}.
   * 
   * @param i18nDynamicValue
   *          the {@link I18nDynamicValue} to inspect
   * @param language
   *          the {@link Language} of interest
   * @return the {@link I18nValue} associated to the given {@link Language}
   */
  private I18nValue getI18nValue(I18nDynamicValue i18nDynamicValue, Language language)
  {
    I18nValue i18nValue = i18nDynamicValue.getI18nValues().get(language);
    if (i18nValue == null) {
      if (i18nDynamicValue instanceof I18nDynamicText) {
        i18nValue = new I18nTextImpl();
        i18nValue.setLanguage(language);
        ((I18nDynamicText) i18nDynamicValue).addI18nValue((I18nText) i18nValue);
      }
      else {
        i18nValue = new I18nStringImpl();
        i18nValue.setLanguage(language);
        ((I18nDynamicString) i18nDynamicValue).addI18nValue((I18nString) i18nValue);
      }
    }
    return i18nValue;
  }



  /**
   * set the value of an {@link I18nDynamicValue} for a given {@link Locale}.
   * 
   * @param i18nDynamicValue
   *          the {@link I18nDynamicValue} to modify
   * @param locale
   *          the {@link Locale} to modify
   * @param value
   *          the new value
   * @throws PersistenceException
   *           if an error occured
   * @see com.genia.toolbox.projects.toolbox_basics_project.spring.manager.I18nDynamicEntityManager#setValue(com.genia.toolbox.projects.toolbox_basics_project.bean.model.I18nDynamicValue,
   *      java.util.Locale, java.lang.String)
   */
  @Transactional(rollbackFor = BundledException.class)
  public void setValue(I18nDynamicValue i18nDynamicValue, Locale locale, String value)
      throws PersistenceException
  {
    Language language = getLanguageManager().getOrCreateLanguage(locale);
    I18nValue i18nValue = getI18nValue(i18nDynamicValue, language);
    i18nValue.setValue(value);
    saveOrUpdate((Persistable<?>) i18nDynamicValue);
  }

}
